<?php
class Document_model extends MY_Model{

    function __construct()
    {
        parent::__construct();
        $this->_table        =    'documents';   //ten table trong db
        $this->primary_key    =    'id';     //khoa chinh
    }
    
    function get_my_documents($userid= null, $limit = 10, $offset = 0,$sql = '')
    {
    	if($sql !=''){
    		$this->db->where($sql);
    	}
    	$rows	=	$this->db->select('d.*')->from($this->_table.' d')->order_by('created_date DESC')->limit($limit,$offset)->get()->result();
    	$result['rows']	=	$rows;
    	if($sql != ''){
    		$this->db->where($sql);
    	}
    	$num_rows	=	$this->db->select('count(d.id) as cnt')->from($this->_table.' d')->get()->row()->cnt;
    	$result['num_rows']	=	$num_rows;
    	return $result;
    }
    
    function get_by_user($user_id = null, $id = null)
    {
    	$row	=	$this->db->select('d.*')->from($this->_table.' d')->join('documents_role dr','dr.document_id=d.id')->where('user_id',$user_id)->where('d.id',$id)->get()->row();
    	//die($this->db->last_query());
    	return $row;
    }
}